<template>
  <el-card>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="域名" prop="domain">
        <el-input v-model="formData.domain" placeholder="请输入域名" />
      </el-form-item>
      <el-form-item label="网站描述" prop="description">
        <el-input v-model="formData.description" type="textarea" placeholder="请输入网站描述" />
      </el-form-item>
      <el-form-item label="网站标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入网站标题" />
      </el-form-item>
      <el-form-item label="icon" prop="icon">
        <UploadImg v-model="formData.icon" />
      </el-form-item>
      <el-form-item label="版权" prop="copyright">
        <el-input v-model="formData.copyright" placeholder="请输入版权" />
      </el-form-item>
      <el-form-item label="关键词" prop="keywords">
        <el-input v-model="formData.keywords" placeholder="请输入关键词" />
      </el-form-item>
    </el-form>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">保 存</el-button>

    <el-button @click="openPreview">预览</el-button>
  </el-card>
</template>
<script setup lang="ts">
import { WebsiteApi, WebsiteVO } from '@/api/cms/website'
import {getAccessToken} from "@/utils/auth";

/** 站点 表单 */
defineOptions({ name: 'WebsiteForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  domain: undefined,
  description: undefined,
  title: undefined,
  icon: undefined,
  copyright: undefined,
  keywords: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

// 页面加载时执行
onMounted(() => {
  console.log('页面加载完成')
  // 可以在这里调用数据初始化方法或接口请求
  initPageData()
})

const token = getAccessToken()
const openPreview=async () => {

  window.open(`${import.meta.env.VITE_BASE_URL}/cms/templates/preview/index?token=Bearer ${token}`)
}

/** 打开弹窗 */
const initPageData = async () => {
  dialogVisible.value = true
  resetForm()
    formLoading.value = true
    try {
      formData.value = await WebsiteApi.getWebsiteByTenantId()
    } finally {
      formLoading.value = false
    }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as WebsiteVO

      await WebsiteApi.insertOrUpdate(data)
      message.success(t('common.updateSuccess'))

    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    domain: undefined,
    description: undefined,
    title: undefined,
    icon: undefined,
    copyright: undefined,
    keywords: undefined,
  }
  formRef.value?.resetFields()
}
</script>
